﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using DevExpress.XtraGrid.Views.Base;
using Contract.Domain;

namespace CIIS
{
    public partial class UserManageControl : BaseUserControl
    {
        public UserManageControl()
        {
            InitializeComponent();
            LoadData();
            LoadTree();
            gridView1.FocusedRowChanged += user_FocusedRowChanged;
        }
        private void LoadTree()
        {
            var tmplist = dao.GetListFromCache<Contract.Domain.Office>();
            IList<Contract.Domain.Office> list = new List<Contract.Domain.Office>();
            IList<Contract.Domain.Office> treeList = new List<Contract.Domain.Office>();
            for (var i = 0; i < tmplist.Count; i++)
            {
                var o = tmplist[i] as Office;
                list.Add(o);
            }
            var tmp = list.Where(m => string.IsNullOrEmpty(m.ParentId)).OrderBy(m => m.Seq).ToList();
            if (tmp.Count > 0)
            {
                foreach (var o in tmp)
                {
                    o.Name = "-"+o.Name;
                    treeList.Add(o);
                    loadChild(list, treeList, o, 1);
                }
            }
            this.repositoryItemLookUpEdit1.DataSource = null;
            repositoryItemLookUpEdit1.DataSource = treeList;
            repositoryItemLookUpEdit1.DisplayMember = "Name";
            repositoryItemLookUpEdit1.ValueMember = "Id";
            repositoryItemLookUpEdit1.Columns.Clear();
            repositoryItemLookUpEdit1.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo()
            {
                Caption = "名称",
                FieldName = "Name"
            });

            repositoryItemLookUpEdit2.DataSource = treeList;
            repositoryItemLookUpEdit2.DisplayMember = "Name";
            repositoryItemLookUpEdit2.NullText = string.Empty;
            repositoryItemLookUpEdit2.ValueMember = "Id";
            repositoryItemLookUpEdit2.Columns.Clear();
            repositoryItemLookUpEdit2.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo()
            {
                Caption = "名称",
                FieldName = "Name"
            });
        }
        private void loadChild(IList<Contract.Domain.Office> all, IList<Contract.Domain.Office> tree, Office current, int depth)
        {
            string s = "|-";
            for (var i = 0; i < depth; i++)
            {
                s += "-";
            }
            var tmp = all.Where(m => !string.IsNullOrEmpty(m.ParentId) && m.ParentId.Equals(current.Id)).OrderBy(m => m.Seq).ToList();
            if (tmp.Count > 0)
            {
                foreach (var o in tmp)
                {
                    o.Name = s + o.Name;
                    tree.Add(o);
                    loadChild(all, tree, o, depth + 1);
                }
            }
        }
        private bool isReload = false;
        public void LoadData()
        {
            DevControlUtils.Instance.InitGridView(gridView1, false);
            if (!isReload)
            {
                this.gridControl1.DataSource = DataAccessObj.Instance.GetListFromCache<Contract.Domain.t_sys_user>();
            }
            else
            {
                this.gridControl1.DataSource = DataAccessObj.Instance.ReloadDataList<Contract.Domain.t_sys_user>();
            }
            gridControl1.RefreshDataSource();
        }
        Contract.Domain.t_sys_user user = null;
        void user_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                user = this.gridView1.GetRow(e.FocusedRowHandle) as Contract.Domain.t_sys_user;
            }
            else
            {
                user = null;

            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (user != null)
            {
                UserEdit edit = new UserEdit(user);
                if (edit.ShowDialog() == DialogResult.OK)
                {
                    isReload = true;
                    LoadData();
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            UserEdit form = new UserEdit();
            if (form.ShowDialog() == DialogResult.OK)
            {
                isReload = true;
                LoadData();
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (user != null)
            {
                if (MessageBox.Show("删除提示", "确认要删除？", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    isReload = true;
                    LoadData();
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的数据！");
            }
        }

    }
}
